<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 17-1-3
 * Time: 上午11:55
 */

namespace mybackend\controllers;

use common\models\common\ErpUserModel;
use common\models\common\OperatorModel;
use common\services\outstock\OutStockFlowService;
use common\services\outstock\OutStockOrderService;
use Yii;
use common\services\outstock\DeliveryOrderFlowService;

/**
 * Class OutStockFlowController
 * @package mybackend\controllers
 * @property \common\services\outstock\OutStockFlowService  $flowService
 * @property \common\services\outstock\OutStockOrderService $orderService
 * @property \common\services\outstock\DeliveryOrderFlowService  $deliveryOrderFlowService
 */
class OutStockFlowController extends BaseController
{
    private $flowService;
    private $orderService;
    private $deliveryOrderFlowService;
    private $deliveryGuid;

    /**
     * @inheritdoc
     */
    public function __construct($id, $module, OutStockOrderService $orderService, $config = [])
    {
        $this->orderService = $orderService;
        parent::__construct($id, $module, $config);
    }

    public function beforeAction($action)
    {
        if (parent::beforeAction($action)) {
            if(in_array($action->actionMethod,['actionOrderQr','actionOrderDh'])) {
                $this->deliveryGuid = \Yii::$app->request->get("delivery_guid", '');
                if(empty($this->deliveryGuid)) {
                    $this->deliveryGuid = \Yii::$app->request->post("delivery_guid", '');
                }
                if (!$this->deliveryGuid) {
                    return $this->exportJson(null, -1, '缺少delivery_guid参数');
                }
                /** @var DeliveryOrderFlowService deliveryOrderService */
                $this->deliveryOrderFlowService = Yii::createObject([
                    'class' => DeliveryOrderFlowService::className(),
                    'operator' => new ErpUserModel([
                        'UserGUID' => $this->getUserIdentity()->userguid,
                        'UserName' => $this->getUserIdentity()->username,
                        'UserCode' => $this->getUserIdentity()->usercode,
                        'DepartmentName' => $this->getUserIdentity()->departmentName,
                        'DepartmentGUID' => $this->getUserIdentity()->departmentGuid,
                        'BUName' => $this->getUserIdentity()->buName,
                        'BUGUID' => $this->getUserIdentity()->buGuid,
                    ]),
                ]);
            }else if(in_array($action->actionMethod,['actionGetApproveRecord'])){
                /** @var DeliveryOrderFlowService deliveryOrderFlowService */
                $this->deliveryOrderFlowService = Yii::createObject([
                    'class' => DeliveryOrderFlowService::className()
                ]);
            }else {
                if (!$orderId = Yii::$app->request->request('orderId')) {
                    return $this->exportJson(null, -1, '未提供 orderId 参数');
                }

                $order = $this->orderService->getOrderById($orderId);

                if (empty($order)) {
                    return $this->exportJson(null, -1, '该单据不存在 orderId:' . $orderId);
                }

                /** @var OutStockFlowService flowService */
                $this->flowService = Yii::createObject([
                    'class' => OutStockFlowService::className(),
                    'order' => $order,
                    'operator' => new OperatorModel([
                        'user_id' => $this->getUserIdentity()->userguid,
                        'username' => $this->getUserIdentity()->username,
                        'department_id' => $this->getUserIdentity()->departmentGuid,
                    ]),
                ]);
            }
        }

        return true;
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/fq 流程-发起
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     * @apiParam formData {string} responseUserId 响应用户ID
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionFq()
    {
        $responseUserId = Yii::$app->request->post('responseUserId', '');

        if (empty($responseUserId)) {
            $this->errorParam(['responseUserId']);
        }

        $data = $this->flowService->actionFQ($responseUserId);

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/sp 流程-审批
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionSp()
    {

        $data = $this->flowService->actionSP();

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/qr 流程-确认
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionQr()
    {

        $data = $this->flowService->actionQR();

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     * 此接口测试用，生产环境不会使用到
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/kt 流程-开通
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     * @apiParam formData {string} orgName 租户名称
     * @apiParam formData {string} orgCode 租户CODE
     * @apiParam formData {string} password 密码
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionKt()
    {
        $orgName = Yii::$app->request->post('orgName', '');
        $orgId = Yii::$app->request->post('orgId', '');
        $orgCode = Yii::$app->request->post('orgCode', '');
        $password = Yii::$app->request->post('password', '');

        $data = $this->flowService->actionKT($orgName, $orgCode, $password);

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/sq 流程-授权
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionSq()
    {
        $data = $this->flowService->actionSQ();

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/ch 流程-撤回
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     * @apiParam formData {string} remarks 备注
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionCh()
    {
        $remarks = Yii::$app->request->post('remarks');

        $data = $this->flowService->actionCH($remarks);

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }

    /*
     *
     * Author yuyj
     * Date 2017-01-03
     * @apiVersion 1.0.0
     * @api {post} /api/index.php/out-stock-flow/sq 流程-授权
     * @apiGroup out-stock-flow-controller
     *
     * @apiParam query {string} orderId 单据ID
     * @apiParam formData {string} remarks 备注
     *
     * @apiResponse 200 {
     *  "retCode": 0,
     *  "errMsg": "",
     *  "data": true
     * }
     */
    public function actionDh()
    {
        $remarks = Yii::$app->request->post('remarks', '备注');

        if (empty($remarks)) {
            return $this->exportJson(null, -1, '备注不能为空');
        }

        $data = $this->flowService->actionDH($remarks);

        return $this->exportJson($data, $this->flowService->getErrCode(), $this->flowService->getErrMsg());
    }
    public function actionOrderQr()
    {
        $data=$this->deliveryOrderFlowService->orderQr($this->deliveryGuid);
        return $this->exportJson($data, $this->deliveryOrderFlowService->getErrCode(), $this->deliveryOrderFlowService->getErrMsg());
    }
    public function actionOrderDh()
    {
        $remarks=\Yii::$app->request->post("remarks",'');
        $data=$this->deliveryOrderFlowService->orderDh($this->deliveryGuid,$remarks);
        return $this->exportJson($data, $this->deliveryOrderFlowService->getErrCode(), $this->deliveryOrderFlowService->getErrMsg());
    }
    public function actionGetApproveRecord()
    {
        $subOrderId=\Yii::$app->request->get("sub_order_id",'');
        $result=$this->deliveryOrderFlowService->getApproveRecord($subOrderId);
        foreach ($result as $key=>$data){
            $result[$key]['create_by']=$data['operator_name'];
            $result[$key]['id']=$data['flow_record_id'];
            $result[$key]['department_name']=$data['operator_buname'];
            $result[$key]['order_id']=$data['sub_order_id'];
        }
        return $this->exportJson($result, $this->deliveryOrderFlowService->getErrCode(), $this->deliveryOrderFlowService->getErrMsg());
    }
}